package com.bix.util.blizfiles.m2;

import java.nio.ByteBuffer;

import org.apache.log4j.Logger;

import com.bix.util.blizfiles.BufferUtils;

/**
 * Represents a texture in a M2 file.
 * 
 * @author squid
 * 
 * @version 1.0.0
 */
public class M2Texture {
	private static final Logger log = Logger.getLogger (M2Texture.class.getName ());

	private int type;

	private int flags;

	//
	// Here is the length of the filename, if the type is not "0 - hardcoded"
	// then it's just 1 and points to a zero.
	//
	private String filename;

	public M2Texture() {
	}

	/**
	 * Instantiate a new texture from a ByteBuffer.
	 * 
	 * @param bb
	 *          The ByteBuffer to read the texture from.
	 */
	public M2Texture(ByteBuffer bb) {
		read (bb);
	}

	/**
	 * Reads a Texture object from a ByteBuffer.
	 * 
	 * @param bb
	 *          The ByteBuffer to read the texture from.
	 */
	public void read (ByteBuffer bb) {
		this.type = bb.getInt ();
		this.flags = bb.getInt ();
		M2CountOffsetPair filenameCOP = new M2CountOffsetPair (bb);

		//
		// Read the filename and then restore the buffer's position back to its
		// original value so we don't screw things up. Normally, the consumer
		// of this class would read anything pointed to by a count/offset pair, but
		// it's just easier to do it this way since it's a single String value.
		//
		this.filename = BufferUtils.getString (bb, filenameCOP, true);

		log.debug ("  Type[" + this.getType () + "]");
		log.debug ("  Flags[" + this.getFlags () + "]");
		log.debug ("  Filename[" + this.getFilename () + "]");
	}

	/**
	 * Return's the texture type.
	 * 
	 * <pre>
	 *   Value       Meaning
	 *     0     Texture given in filename
	 *     1     Body + clothes
	 *     2     Cape
	 *     6     Hair, beard
	 *     8     Tauren fur
	 *     11    Skin for creatures #1
	 *     12    Skin for creatures #2
	 *     13    Skin for creatures #3
	 * </pre>
	 * 
	 * @return The texture type.
	 */
	public int getType () {
		return type;
	}

	/**
	 * Return the texture's filename. This could be empty if the texture is
	 * defined elsewhere (i.e. the model's DBC file).
	 * 
	 * @return The texture filename.
	 */
	public String getFilename () {
		return filename;
	}

	/**
	 * Returns the texture flags.
	 * 
	 * <pre>
	 *  Value        Meaning
	 *    1      Texture wrap X
	 *    2      Texture wrap Y
	 * </pre>
	 * 
	 * @return The texture flags
	 */
	public int getFlags () {
		return flags;
	}
}
